#ifndef DB_H
#define DB_H

#include <QDateTime>
#include <QSqlDatabase>
#include <QMutex>
#include <vector>
#include <map>
#include <stack>
class qqbuddy;
class qqqun;
class qunmember;
typedef struct _Status
{
    qqbuddy* b;
    int laststatus;
} Status;
struct STR_inforecord
{
    uint type;
    uint toid;
    uint toqunid;
    uint fromid;
    uint time;
    QString info;
};

struct STR_qunmember
{
    uint qid;
    qunmember* m;
};
typedef std::vector<QString> AccountList;
typedef std::vector<STR_qunmember> QunMemberRecords;
typedef std::vector<STR_inforecord> InfoRecords;
typedef std::map<uint,InfoRecords*> GroupByUIdInfoRecords;
struct infoqueque
{
    uint number; //号码
    uint type; //类型,0 buddy,1 qun
};

typedef std::stack<infoqueque> MsgQueque;

class db
{
public:

    db();
    ~db();
    static db& GetInstance()
    {
        return instance;
    }
    void pushQun(qqqun* q);
    void pushRecord(uint type,uint friendId,STR_inforecord &info);//压入信息到缓冲区
    void pushMemberInfo(STR_qunmember &qm);
    void close();
    bool open(uint uid);
    bool getQunMemberName(uint qun_number,uint number,char *name);
    bool writeQunMember(uint qun_number,uint number,const char* name);
    bool writeRecord(uint type,uint &fromid,uint &toid, uint toqunid,uint time,QString& info);
    bool writeAccount(QString& uid,QString &pwd);
    void readAccount( AccountList &accountList );
    uint getRecordPageCount();
    void setRecordPageSize(uint pageSize);
    uint readRecord(InfoRecords& infoRecords, uint ownerId, uint uid,uint type, uint pageIndex=0);//读取历史记录
    void procRecord(uint friendId);//读缓冲区信息
    void BeginTrans();
    void Commit();
    QMutex mutex_chat;
    QMutex mutex_qunmember;
    QMutex mutex_qun;
    QMutex mutex_db;
    QMutex mutex_status;

private:
    uint m_PageSize;
    uint m_UID;
private:
    static db instance;
    QSqlDatabase m_db;
    uint lastUid;
};

#endif // DB_H
